package com.beardedhen.harmonybootstrap;

import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapSize;
import com.beardedhen.harmonybootstrap.utils.ResUtil;
import com.beardedhen.harmonybootstrap.utils.UiUtil;
import com.beardedhen.harmonybootstrap.utils.ViewUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.StackLayout;
import ohos.agp.components.element.Element;
import ohos.app.Context;

public class BootstrapWell extends StackLayout {

    private static final String TAG = BootstrapWell.class.getSimpleName();

    private float bootstrapSize;
    private String BOOTSTRAP_WELL_BOOTSTRAP_SIZE = "bootstrapSize";

    public BootstrapWell(Context context) {
        super(context);
        initialise(null);
    }

    public BootstrapWell(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initialise(attrSet);
    }

    public BootstrapWell(Context context, AttrSet attrSet, String defStyleAttr) {
        super(context, attrSet, defStyleAttr);
        initialise(attrSet);
    }

    private void initialise(AttrSet attrSet) {
        if(attrSet == null) {
            return;
        }
        int sizeOrdinal = -1;
        if (attrSet.getAttr(BOOTSTRAP_WELL_BOOTSTRAP_SIZE).isPresent()) {
            String sizeOrdinalStr = attrSet.getAttr(BOOTSTRAP_WELL_BOOTSTRAP_SIZE).get().getStringValue().trim();
            sizeOrdinal = UiUtil.getBootstrapSizeInt(sizeOrdinalStr);
        }
        bootstrapSize = DefaultBootstrapSize.fromAttributeValue(sizeOrdinal).scaleFactor();
        updateBootstrapState();
    }

    private void updateBootstrapState() {
        Element backgroundElement = BootstrapDrawableFactory.bootstrapWell(ResUtil.getColor(getContext(), ResourceTable.Color_bootstrap_well_background),
                (int) (ResUtil.getDimen(getContext(), ResourceTable.Float_bootstrap_well_corner_radius) * bootstrapSize / 2),
                (int) ResUtil.getDimen(getContext(), ResourceTable.Float_bootstrap_well_stroke_width),
                ResUtil.getColor(getContext(), ResourceTable.Color_bootstrap_well_border_color));

        ViewUtils.setBackgroundDrawable(this, backgroundElement);

        int padding = (int) (ResUtil.getDimen(getContext(), ResourceTable.Float_bootstrap_well_default_padding) * bootstrapSize * 2.5);
        setPadding(padding, padding, padding, padding);
    }
}
